﻿using System.Windows.Input;

namespace Xioa_UI.Service;

public class XioaRelayAsyncCommandGenerics<T> : ICommand
{
    private Func<T?, Task>? _execute;

    private Func<object, bool>? _canExecute;

    public event EventHandler? CanExecuteChanged;

    public XioaRelayAsyncCommandGenerics(Func<T?, Task>? execute, Func<object, bool>? canExecute = null)
    {
        this._execute = execute;

        this._canExecute = canExecute;
    }

    public bool CanExecute(object? parameter)
    {
        if (_canExecute != null)
            return parameter != null && _canExecute(parameter);

        return true;
    }

    public async void Execute(object? parameter)
    {
        await _execute?.Invoke((T)parameter!)!; 
    }

    public void RaiseCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty); 
    }
}